package com.orphan.marketFront.controller;


import com.orphan.marketFront.po.User;
import com.orphan.marketFront.service.IUserService;
import com.orphan.marketFront.util.DecryptMD5;
import com.orphan.marketFront.util.JwtUtil;
import com.orphan.marketFront.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;


@Api(tags = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService userService;

    @ApiOperation(value = "测试数据", notes = "测试数据")
    @RequestMapping("test")
    public String getData() {
        return "String";
    }

    @ApiOperation(value = "用户快速注册", notes = "用户快速注册")
    @PostMapping("/register")
    public Map<String, Object> register(@RequestParam String userName,
                                        @RequestParam String passwd,
                                        @RequestParam String realName,
                                        @RequestParam String tel,
                                        @RequestParam String gender) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(realName)) {
            map.put("msg", StringUtil.REALNAME_NOTNULL_ERROR);
            return map;
        }else if(!realName.matches("^([\\u4e00-\\u9fa5]{1,20}|[a-zA-Z\\.\\s]{1,20})$")){
            map.put("msg", StringUtil.REALNAME_FORMAT_ERROR);
            return map;
        }
        if (StringUtils.isEmpty(tel)) {
            map.put("msg", StringUtil.TEL_NOTNULL_ERROR);
            return map;
        }else if(!tel.matches("^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$")){
            map.put("msg", StringUtil.TEL_FORMAT_ERROR);
            return map;
        }

        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(passwd)) {
            map.put("msg", StringUtil.USERNAMEANDPASSWORD_NOTNULL_ERROR);
            return map;
        }
        else if(!userName.matches("^[\u4e00-\u9fa5a-zA-Z0-9]{1,10}$")){
            map.put("msg", StringUtil.USERNAME_FORMAT_ERROR);
            return map;
        }
        else if(!passwd.matches("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])[A-Za-z0-9]{6,20}$")){
            map.put("msg", StringUtil.PASSWORD_FORMAT_ERROR);
            return map;
        }
        // 验证用户名是否已经注册
        User exsitUser = userService.selectByName(userName);
        if (exsitUser != null) {
            map.put("msg", StringUtil.USER_EXIST_ERROR);
            return map;
        }
        System.out.println(gender);

//        User user = new User();
//
//        user.setUserName(userName);
//        user.setRealName(realName);
//        user.setGender(gender);
//        user.setTel(Long.parseLong(tel));
//        user.setPassword(DecryptMD5.KL(DecryptMD5.MD5((passwd) + userName)));
//
//        int count = userService.insert(user);
//        System.out.println(count);
//        if (count != 1) {
//            map.put("msg", StringUtil.REGISTER_FAILURE_ERROR);
//            return map;
//        }
        map.put("msg", StringUtil.REGISTER_SUCCESS);
        return map;
    }

    @ApiOperation(value = "用户登录", notes = "用户登录")
    @PostMapping(value = "/login")
    public Map<String, Object> login(@RequestParam String userName,
                                     @RequestParam String passwd,
                                     HttpSession session,
                                     HttpServletResponse response) {

        Map<String, Object> map = new HashMap<String, Object>();
        if (StringUtils.isEmpty(userName)) {
            map.put("msg", StringUtil.USERNAME_NOTNULL_ERROR);
            return map;
        }
        if (StringUtils.isEmpty(passwd)) {
            map.put("msg", StringUtil.PASSWORD_NOTNULL_ERROR);
            return map;
        }

        User exsitUser = userService.selectByName(userName);
        if (exsitUser == null) {
            map.put("msg", StringUtil.USER_NOTEXIST_ERROR);
            return map;
        }
        if (!exsitUser.getPassword().equals(DecryptMD5.KL(DecryptMD5.MD5((passwd) + userName)))) {
            map.put("msg", StringUtil.PASSWORD_INCORRECT);
            return map;
        }
        session.setAttribute("loginUser", exsitUser);
        map.put("msg", StringUtil.LOGIN_SUCCESS);
        map.put("token", JwtUtil.createToken(userName));
        return map;
    }

    @GetMapping("checkToken")
    public String checkToken(HttpServletRequest request){
        //从请求头获得token
        String token=   request.getHeader("token");
        //验证token
        String userName= JwtUtil.verifierJwt(token);
        if(!userName.equals("")){
//            System.out.println(userName);
            return userName;
        }else {
            return "token过期";
        }
    }
}
